﻿using System;
using Lucene.Net.Analysis;
using System.Text;
using System.IO;
using TermAttribute = Lucene.Net.Analysis.Tokenattributes.TermAttribute;
using Version = Lucene.Net.Util.Version;
using System.Collections.Generic;

namespace OMC.DataManagement
{
    /// <summary>
    /// This class represent the lucene analyzer to analyze text.
    /// </summary>
    class LuceneAnalyzer : IDisposable
    {
        private Analyzer analyzer;
        private Analyzer analyzerUsesStopWords;
        private TokenStream tokenStream;
        private StringBuilder stringBuilder;
        private Version luceneVersion;
        private static readonly List<String> STOP_WORDS = new List<string>();
        private static readonly List<String> STOP_WORDS_GERMAN = new List<string>();

        /// <summary>
        /// The constructor of the LuceneAnalyzer.
        /// </summary>
        public LuceneAnalyzer()
        {
            BuildStoppWordList();
            luceneVersion = new Version("LUCENE", 35);
            analyzerUsesStopWords = new Lucene.Net.Analysis.Standard.StandardAnalyzer(luceneVersion, STOP_WORDS_GERMAN);
            analyzer = new Lucene.Net.Analysis.Standard.StandardAnalyzer(luceneVersion, STOP_WORDS);
            stringBuilder = new StringBuilder();
        }

        public void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(this);
        }

        protected virtual void Dispose(bool disposing)
        {
            if (disposing)
            {
                // free managed resources
                if (analyzer != null)
                {
                    analyzer.Dispose();
                    analyzer = null;
                }
                if (analyzerUsesStopWords != null)
                {
                    analyzerUsesStopWords.Dispose();
                    analyzerUsesStopWords = null;
                }
            }
        }

        /// <summary>
        /// This Method gets the analyzed string of a text.
        /// </summary>
        /// <param name="text">the String to analyze</param>
        /// <returns>the analyzed String</returns>
        public String GetAnalyzedText(String text)
        {
            stringBuilder.Clear();

            using (StringReader stringReader = new StringReader(text))
            {
                tokenStream = analyzer.TokenStream(null, stringReader);
                TermAttribute termAttribute = tokenStream.AddAttribute(typeof(TermAttribute)) as TermAttribute;
                while (tokenStream.IncrementToken())
                {
                    stringBuilder.AppendFormat("{0} ", termAttribute.Term());
                }
            }
            return stringBuilder.ToString();
        }

        /// <summary>
        /// This method gets analyzed string of a text using the stopwordlist.
        /// </summary>
        /// <param name="text">the String to analyze</param>
        /// <returns>the analyzed String</returns>
        public String GetAnalyzedTextUsingStopwords(String text)
        {
            stringBuilder.Clear();
            using (StringReader stringReader = new StringReader(text))
            {
                tokenStream = analyzerUsesStopWords.TokenStream(null, stringReader);
                TermAttribute termAttribute = tokenStream.AddAttribute(typeof(TermAttribute)) as TermAttribute;
                while (tokenStream.IncrementToken())
                {
                    stringBuilder.AppendFormat("{0} ", termAttribute.Term());
                }
            }
            return stringBuilder.ToString();
        }

        /// <summary>
        /// This method build the stopwordlists.
        /// </summary>
        private void BuildStoppWordList()
        {
            STOP_WORDS.Add("class");
            STOP_WORDS.Add("id");
            STOP_WORDS.Add("date");
            STOP_WORDS.Add("data");
            foreach (String temp in STOP_WORDS)
            {
                STOP_WORDS_GERMAN.Add(temp);
            }
            STOP_WORDS_GERMAN.Add("einer");
            STOP_WORDS_GERMAN.Add("eine");
            STOP_WORDS_GERMAN.Add("eines");
            STOP_WORDS_GERMAN.Add("einem");
            STOP_WORDS_GERMAN.Add("einen");
            STOP_WORDS_GERMAN.Add("der");
            STOP_WORDS_GERMAN.Add("die");
            STOP_WORDS_GERMAN.Add("das");
            STOP_WORDS_GERMAN.Add("dass");
            STOP_WORDS_GERMAN.Add("du");
            STOP_WORDS_GERMAN.Add("er");
            STOP_WORDS_GERMAN.Add("sie");
            STOP_WORDS_GERMAN.Add("es");
            STOP_WORDS_GERMAN.Add("was");
            STOP_WORDS_GERMAN.Add("wer");
            STOP_WORDS_GERMAN.Add("wie");
            STOP_WORDS_GERMAN.Add("wir");
            STOP_WORDS_GERMAN.Add("und");
            STOP_WORDS_GERMAN.Add("oder");
            STOP_WORDS_GERMAN.Add("ohne");
            STOP_WORDS_GERMAN.Add("mit");
            STOP_WORDS_GERMAN.Add("am");
            STOP_WORDS_GERMAN.Add("im");
            STOP_WORDS_GERMAN.Add("in");
            STOP_WORDS_GERMAN.Add("aus");
            STOP_WORDS_GERMAN.Add("auf");
            STOP_WORDS_GERMAN.Add("ist");
            STOP_WORDS_GERMAN.Add("sein");
            STOP_WORDS_GERMAN.Add("war");
            STOP_WORDS_GERMAN.Add("wird");
            STOP_WORDS_GERMAN.Add("ihr");
            STOP_WORDS_GERMAN.Add("ihre");
            STOP_WORDS_GERMAN.Add("ihres");
            STOP_WORDS_GERMAN.Add("als");
            STOP_WORDS_GERMAN.Add("von");
            STOP_WORDS_GERMAN.Add("dich");
            STOP_WORDS_GERMAN.Add("dir");
            STOP_WORDS_GERMAN.Add("mich");
            STOP_WORDS_GERMAN.Add("mir");
            STOP_WORDS_GERMAN.Add("mein");
            STOP_WORDS_GERMAN.Add("klein");
            STOP_WORDS_GERMAN.Add("durch");
            STOP_WORDS_GERMAN.Add("wegen");
            STOP_WORDS_GERMAN.Add("fÃ¼r");
        }
    }
}